//
//  HYQDatePickerView.m
//  HYQPickerView_Example
//
//  Created by zhuxingjian on 2018/8/28.
//  Copyright © 2018年 zxj1989. All rights reserved.
//

#import "HYQDatePickerView.h"
#import "HYQDatePickerHelper.h"

#define HYQ_DATE_PICKER_YEAR_MIN  1000
#define HYQ_DATE_PICKER_YEAR_MAX  3000

@interface HYQDatePickerView ()

@property (nonatomic, strong) NSArray *daysArray;

@property (nonatomic, assign) NSInteger selectedYear;
@property (nonatomic, assign) NSInteger selectedMonth;
@property (nonatomic, assign) NSInteger selectedDay;
@property (nonatomic, assign) NSInteger selectedHour;
@property (nonatomic, assign) NSInteger selectedMinute;
@property (nonatomic, assign) NSInteger selectedSecond;

@end

@implementation HYQDatePickerView

- (void)setMode:(HYQDatePickerViewMode)mode
{
    _mode = mode;
    NSArray *years = [HYQDatePickerHelper yearArrayWithMin:HYQ_DATE_PICKER_YEAR_MIN max:HYQ_DATE_PICKER_YEAR_MAX];
    NSArray *month = [HYQDatePickerHelper monthArray];
    
    NSDate *date = self.selectedDate ? : [NSDate date];
    NSDictionary *dateInfo = [HYQDatePickerHelper getDateInfoFromDate:date];
    self.daysArray = [HYQDatePickerHelper daysArrayForYear:[dateInfo[@"year"] integerValue] month:[dateInfo[@"month"] integerValue]];
    if (mode == HYQDatePickerViewModeDate) {
        self.componentDataArray = @[years, month, self.daysArray];
    }else {
        NSArray *hours = [HYQDatePickerHelper hoursArray];
        NSArray *minutes = [HYQDatePickerHelper minutesArray];
        self.componentDataArray = @[years, month, self.daysArray, hours, minutes];
    }
    self.selectedDate = date;
}

- (void)setSelectedDate:(NSDate *)selectedDate
{
    if (!selectedDate) {
        return;
    }
    _selectedDate = selectedDate;
    if (self.componentDataArray.count) {
        NSDictionary *dateInfo = [HYQDatePickerHelper getDateInfoFromDate:selectedDate];
        self.daysArray = [HYQDatePickerHelper daysArrayForYear:[dateInfo[@"year"] integerValue] month:[dateInfo[@"month"] integerValue]];
        
        self.selectedYear = [dateInfo[@"year"] integerValue];
        self.selectedMonth = [dateInfo[@"month"] integerValue];
        self.selectedDay = [dateInfo[@"day"] integerValue];
        
        [self setRowSelected:[dateInfo[@"year"] integerValue] - HYQ_DATE_PICKER_YEAR_MIN component:0];
        [self setRowSelected:[dateInfo[@"month"] integerValue] - 1 component:1];
        [self setRowSelected:[dateInfo[@"day"] integerValue] - 1 component:2];
        
        if (self.mode == HYQDatePickerViewModeAndMinute) {
            self.selectedHour = [dateInfo[@"hour"] integerValue];
            self.selectedMinute = [dateInfo[@"minute"] integerValue];
            
            [self setRowSelected:[dateInfo[@"hour"] integerValue] component:3];
            [self setRowSelected:[dateInfo[@"minute"] integerValue] component:4];
        }
    }
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component;
{
    if (component == 2) {
        return self.daysArray.count;
    }
    return [self.componentDataArray[component] count];
}

- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view{
    UILabel *pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        for (UIView *singleLine in pickerView.subviews) {
            if (singleLine.frame.size.height < 1) {
                singleLine.backgroundColor = [UIColor clearColor];
            }
        }
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.backgroundColor = [UIColor redColor];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont systemFontOfSize:17]];
    }
    if (component == 2) {
        pickerLabel.text = [NSString stringWithFormat:@"%@",self.daysArray[row]];
    }else {
        pickerLabel.text = [NSString stringWithFormat:@"%@",[self.componentDataArray[component] objectAtIndex:row]];
    }
    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    NSInteger year = component == 0 ? row + HYQ_DATE_PICKER_YEAR_MIN : self.selectedYear;
    NSInteger month = component == 1 ? row + 1 : self.selectedMonth;
    NSInteger day = component == 2 ? row + 1 : self.selectedDay;
    NSInteger hour = component == 3 ? row : self.selectedHour;
    NSInteger minute = component == 4 ? row : self.selectedMinute;
    
    NSDate *date = [HYQDatePickerHelper dateWithYear:year month:month day:day hour:hour minute:minute second:0];
    if ((self.minDate && [date compare:self.minDate] == NSOrderedAscending) || [date compare:self.maxDate] == NSOrderedDescending) {
        if (component == 0) {
            [self setRowSelected:self.selectedYear - HYQ_DATE_PICKER_YEAR_MIN component:0];
        }
        if (component == 1) {
            [self setRowSelected:self.selectedMonth - 1 component:1];
        }
        if (component == 2) {
            [self setRowSelected:self.selectedDay - 1 component:2];
        }
        if (component == 3) {
            [self setRowSelected:self.selectedHour component:3];
        }
        if (component == 4) {
            [self setRowSelected:self.selectedMinute component:4];
        }
        return;
    }
    if (component == 0) {
        self.selectedYear = row + HYQ_DATE_PICKER_YEAR_MIN;
    }
    if (component == 1) {
        self.selectedMonth = row + 1;
    }
    if (component == 1) {
        self.daysArray = [HYQDatePickerHelper daysArrayForYear:self.selectedYear month:self.selectedMonth];
        [pickerView reloadComponent:2];
        [self setRowSelected:self.selectedDay - 1 component:2];
    }
    if (component == 2) {
        self.selectedDay = row + 1;
    }
    if (component == 3) {
        self.selectedHour = row;
    }
    if (component == 4) {
        self.selectedMinute = row;
    }
}

- (void)btnAction:(UIButton *)btn
{
    if (btn.tag == 101) {
        
        NSString *monthStr = self.selectedMonth < 10 ? [NSString stringWithFormat:@"0%zd",self.selectedMonth] : [NSString stringWithFormat:@"%zd",self.selectedMonth];
        NSString *dayStr = self.selectedDay < 10 ? [NSString stringWithFormat:@"0%zd",self.selectedDay] : [NSString stringWithFormat:@"%zd",self.selectedDay];
        NSString *hourStr = self.selectedHour < 10 ? [NSString stringWithFormat:@"0%zd",self.selectedHour] : [NSString stringWithFormat:@"%zd",self.selectedHour];
        NSString *minuteStr = self.selectedMinute < 10 ? [NSString stringWithFormat:@"0%zd",self.selectedMinute] : [NSString stringWithFormat:@"%zd",self.selectedMinute];
        NSString *str = [NSString stringWithFormat:@"%zd-%@-%@ %@:%@:00",self.selectedYear, monthStr, dayStr, hourStr, minuteStr];
        
        NSDate *date = [HYQDatePickerHelper formatDate:str];
        
        NSLog(@"%@", str);
        NSLog(@"%@", date);
        if (self.datePickerViewDidSelectedDateBlock) {
            self.datePickerViewDidSelectedDateBlock(date);
        }
        
    }
    [self hide];
}




/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
